package cn.tedu._02notice.controller;


import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.dto.UserLoginParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListQuery;
import cn.tedu._02notice.pojo.vo.NoticeListVO;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j  //在类中创建一个一个日志对象，调用log的方法来打印各种级别的日志
/*log.trace
 * log.debug   开发过程中使用
 * log.info
 * log.warn
 * log.error   开发过程中使用。全局处理时候处理
 * */
@Controller
@RestController
@RequestMapping("/v1/notice")
@Tag(name= "01-资讯管理模块")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;


    @PostMapping("/add")
    @Operation(summary = "添加资讯")
//    @Parameters({
//            @Parameter(name = "title", description = "资讯标题", required = true),
//            @Parameter(name = "content", description = "资讯内容", required = true),
//            @Parameter(name = "type", description = "资讯类型", required = true),
//            @Parameter(name = "status", description = "资讯状态", required = true),
//            @Parameter(name ="noticeAddParam",hidden = true)
//    })
//    @ResponseBody
    public JsonResult addNotice(NoticeAddParam noticeAddParam) {
        log.debug("添加咨询" + noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setId(1L);
        notice.setTitle(noticeAddParam.getTitle());
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setUserId(1L);
        noticeMapper.insertNotice(notice);
        return new JsonResult(StatusCode.OPERATION_SUCCESS, StatusCode.OPERATION_SUCCESS.getMessage());
//        return new JsonResult(10100,"新增资讯成功",null);
//        return "新增咨询成功";
    }

    @GetMapping("/list")

//    @ResponseBody
    public JsonResult selectNoticeList(NoticeListQuery noticeListQuery) {
        log.debug("查询咨询列表" + noticeListQuery);
        NoticeListVO noticeListVO = new NoticeListVO();
        noticeListVO.setId(1L);
        noticeListVO.setType(noticeListQuery.getType());
        noticeListVO.setTitle(noticeListQuery.getTitle());
        List<NoticeListVO> noticeListVOS = noticeMapper.selectNoticeList();
        for (NoticeListVO noticeListVO1 : noticeListVOS) {
            System.out.println(noticeListVO1);
        }
        return JsonResult.ok();

//        return new JsonResult(10100,"查询成功",noticeListVOS);
//        return "查询成功";

    }

    @GetMapping("/detail")
    @Operation(summary = "查询资讯列表")
    @ApiOperationSupport(order =30)
    @Parameters({
            @Parameter(name = "id", description = "咨询类型", required = true),
            @Parameter(name = "username", description = "咨询标题", required = true ,hidden = true)
    })
//    @ResponseBody
    public String selectDetailInfo(Long id,String username ) {

        Notice notice = new Notice();
        NoticeDetailInfoVO noticeDetailInfoVO1 = noticeMapper.selectDetailInfo(id);
        System.out.println(noticeDetailInfoVO1);
        BeanUtils.copyProperties(noticeDetailInfoVO1, notice);
        System.out.println(notice);
        return "查询成功";
    }

    @DeleteMapping("/delete")
//    @ResponseBody
    public String delNotice(Long id) {
        System.out.println("id:" + id);
        int i = noticeMapper.deleteNoticeById(id);
        if (i > 0) {
            return "删除资讯成功";
        }
        return "删除咨询失败";
    }

    @PostMapping("/update")
    @Operation(summary = "更新资讯")
//    @ResponseBody
    public String updateNotice(NoticeUpdateParam noticeUpdateParam) {
        System.out.println("noticeUpdateParm = " + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        System.out.println("notice =" + notice);
        int i = noticeMapper.updateNoticeById(notice);
        if (i > 0) {
            return "修改资讯成功";
        }
        return "修改失败，该资讯不存在";
    }

    @PostMapping("/login")
    public String login(UserLoginParam userLoginParam) {
        log.debug("userLoginParam=" + userLoginParam);
        return "登录成功";
    }

}
